//
//  StockChargeSyncService.m
//  Stock
//
//  Created by black sun on 12-3-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "StockChargeSyncService.h"

@implementation StockChargeSyncService

-(void)signupUser:(NSString *)email passwd:(NSString *)passwd{
    NSString *urlStr = [NSString stringWithFormat:@"%@/srv/iphone/user/signin?u=%@&e=%@&p=%@",kHostName,[StockContext shareInstance].userId,email,passwd];
    NSLog(@"%@",urlStr);
    NSURL *url = [NSURL URLWithString:urlStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:email,ChargeUserNameKey,passwd,ChargePasswordKey,ChargeSycnSignup,kASIHttpRequestUserinfoKey, nil];
    request.userInfo = userInfo;
    [workingRequests addObject:request];
    [request startSynchronous];
}
-(void)loginAndSync:(NSString *)email passwd:(NSString *)passwd{
    NSString *urlStr = [NSString stringWithFormat:@"%@/srv/iphone/user/checkin?u=%@&e=%@&p=%@&b=1",kHostName,[StockContext shareInstance].userId,email,passwd];
    NSURL *url = [NSURL URLWithString:urlStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:email,ChargeUserNameKey,passwd,ChargePasswordKey,ChargeSycnLogin,kASIHttpRequestUserinfoKey, nil];
    request.userInfo = userInfo;
    [workingRequests addObject:request];
    [request startSynchronous];
}
-(void)modifyPasswd:(NSString *)email passwd:(NSString *)passwd{
    NSString *urlStr = [NSString stringWithFormat:@"%@/srv/iphone/user/password?u=%@&e=%@&p=%@",kHostName,[StockContext shareInstance].userId,email,passwd];
    NSURL *url = [NSURL URLWithString:urlStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:email,ChargeUserNameKey,passwd,ChargePasswordKey,ChargeSycnModifyPass,kASIHttpRequestUserinfoKey, nil];
    request.userInfo = userInfo;
    [workingRequests addObject:request];
    [request startSynchronous];
}



-(void)requestFinished:(ASIHTTPRequest *)request{
    //NSString *action = [request.userInfo objectForKey:kASIHttpRequestUserinfoKey];
    NSDictionary *json = [request.responseString JSONValue];
    NSString *exception = [json objectForKey:@"exception"];
    if (exception!=nil) {
        NSError *error = [NSError errorWithDomain:exception code:-1 userInfo:nil];
        [self.delegate requestFinishedwithError:error andUserinfo:request.userInfo];
        return;
    }
    id success = [json objectForKey:@"success"];
    id radarDays = [json objectForKey:@"radarDays"];
    if (success==nil && radarDays==nil) {
        NSError *error = [NSError errorWithDomain:@"网络错误或请求错误" code:-1 userInfo:nil];
        [self.delegate requestFinishedwithError:error andUserinfo:request.userInfo];
        return;
    }
    if (radarDays!=nil) {
        [StockContext shareInstance].radarDays = [[json objectForKey:@"radarDays"] intValue];
        [StockContext shareInstance].radarTest = [[json objectForKey:@"radarTest"] boolValue];
        [StockContext shareInstance].paid = [[json objectForKey:@"paid"] boolValue];
        NSString *date = [json objectForKey:@"radarExpiryDate"];
        [[StockContext shareInstance] updateaRadarDate:date];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kNeedUpdateRadarDateNotification object:nil];
    }
    
    [[StockContext shareInstance] saveChargeEmail:[request.userInfo objectForKey:ChargeUserNameKey]];
    [[StockContext shareInstance] saveChargePassword:[request.userInfo objectForKey:ChargePasswordKey]];
    
    [self.delegate requestFinishedWithResult:json andUserinfo:request.userInfo];
}
-(void)requestFailed:(ASIHTTPRequest *)request{
    [self.delegate requestFinishedwithError:request.error andUserinfo:request.userInfo];
}

@end
